<?php

include('../includes/smarty.php');
include_once('../config.php');

session_start();

$ADMIN = new Admin();
$EMPLOYER = new Employer();
$INDUSTRY = new Industry();
$EXPERIENCE = new Experience();
$JOBS = new Jobs();

if(isset($_SESSION["id"]))
{
	$row = $ADMIN->validate_admin_session($_SESSION["id"]);
	
	if($row)
	{
		if(empty($_POST))
		{
			//	Show the screen.
			$ADMIN->update_lastvisit($_SESSION["id"]);
		
			$smarty->assign('title', 		'Admin: Select a Company');
			$smarty->assign('path', 		$site);
			$smarty->assign('body', 		'main/admin/selectCompany.tpl');
			$smarty->assign('companylist', 	$EMPLOYER->get_companies());
			$smarty->assign('message',		'');
		
			$smarty->display('admin.tpl');
		}
		else
		{
			if(isset($_SESSION['modID']))
			{				
				if(!empty($_POST['companyID']) && $_SESSION['modID'] == $_POST['companyID'])
				{
					$companyID	= $_POST['companyID'];
					$name 		= $_POST['name'];
					$industry	= $_POST['industry'];
					$address 	= $_POST['address'];
					$city		= $_POST['city'];
					$state		= $_POST['state'];
					$zip		= $_POST['zip'];
					$address2	= $_POST['address2'];
					$city2		= $_POST['city2'];
					$state2		= $_POST['state2'];
					$zip2		= $_POST['zip2'];
					$phone		= $_POST['phone'];
					$fax		= $_POST['fax'];
					$website	= $_POST['website'];
					$email		= $_POST['email'];
	
					$EMPLOYER->update_company($companyID, $name, $industry, $address, $city, $state, $zip, $address2, $city2, $state2, $zip2, $phone, $fax, $website, $email);;
	
					$smarty->assign('message', 	'Successfully Updated the Company');	
			
					SmartyValidate::connect($smarty, true);
		
					SmartyValidate::register_validator('rv_companyName',	'companyName',			'notEmpty',	false, 	false, 'trim');
					SmartyValidate::register_validator('rv_address',		'address',				'notEmpty',	false, 	false, 'trim');
					SmartyValidate::register_validator('rv_city',		 	'city',					'notEmpty',	false, 	false, 'trim');
					SmartyValidate::register_validator('rv_zip',		 	'zip',					'isNumber',	false, 	false, 'trim');
					SmartyValidate::register_validator('rv_address2',	 	'address2',				'notEmpty',	true, 	false, 'trim');
					SmartyValidate::register_validator('rv_city2',		 	'city2',				'notEmpty',	true, 	false, 'trim');
					SmartyValidate::register_validator('rv_zip2',		 	'zip2',					'isNumber',	true, 	false, 'trim');
					SmartyValidate::register_validator('rv_email', 			'email', 				'isEmail', 	false, 	false, 'trim');
		
					$smarty->assign('states', 	get_state_select());
					$smarty->assign('industries', $INDUSTRY->get_industry_select());
					$smarty->assign('zipREX', 	$regex_zip);
					$smarty->assign('phoneREX', $regex_phone);
					$smarty->assign('emailREX', $regex_email);		
					$smarty->assign('title', 	'Admin: Update Selected Company');
					$smarty->assign('path', 	$site);
					$smarty->assign('body', 	'main/admin/updateCompany.tpl');
					$smarty->assign('companyID', $_POST['companyID']);
					$smarty->assign('name',		$name);
					$smarty->assign('industry',	$industry);
					$smarty->assign('address',	$address);
					$smarty->assign('city',		$city);
					$smarty->assign('state',	$state);
					$smarty->assign('zip',		$zip);
					$smarty->assign('address2',	$address2);
					$smarty->assign('city2',	$city2);
					$smarty->assign('state2',	$state2);
					$smarty->assign('zip2',		$zip2);
					$smarty->assign('phone',	$phone);
					$smarty->assign('fax',		$fax);
					$smarty->assign('website',	$website);
					$smarty->assign('email',	$email);
		
					$smarty->display('admin.tpl');
				}
				else
				{
					$_SESSION['modID'] = $_POST['companyName'];
					$companyInfo = $EMPLOYER->get_company_info($_POST['companyName']);
					
					$name 		= $companyInfo['name'];
					$industry	= $companyInfo['industry'];
					$address 	= $companyInfo['address'];
					$city		= $companyInfo['city'];
					$state		= $companyInfo['state'];
					$zip		= $companyInfo['zip'];
					$address2	= $companyInfo['address2'];
					$city2		= $companyInfo['city2'];
					$state2		= $companyInfo['state2'];
					$zip2		= $companyInfo['zip2'];
					$phone		= $companyInfo['phone'];
					$fax		= $companyInfo['fax'];
					$website	= $companyInfo['website'];
					$email		= $companyInfo['email'];
			
					$smarty->assign('message', 	'');
			
					SmartyValidate::connect($smarty, true);
		
					SmartyValidate::register_validator('rv_companyName',	'companyName',			'notEmpty',	false, 	false, 'trim');
					SmartyValidate::register_validator('rv_address',		'address',				'notEmpty',	false, 	false, 'trim');
					SmartyValidate::register_validator('rv_city',		 	'city',					'notEmpty',	false, 	false, 'trim');
					SmartyValidate::register_validator('rv_zip',		 	'zip',					'isNumber',	false, 	false, 'trim');
					SmartyValidate::register_validator('rv_address2',	 	'address2',				'notEmpty',	true, 	false, 'trim');
					SmartyValidate::register_validator('rv_city2',		 	'city2',				'notEmpty',	true, 	false, 'trim');
					SmartyValidate::register_validator('rv_zip2',		 	'zip2',					'isNumber',	true, 	false, 'trim');
					SmartyValidate::register_validator('rv_email', 			'email', 				'isEmail', 	false, 	false, 'trim');
		
					$smarty->assign('states', 	get_state_select());
					$smarty->assign('industries', $INDUSTRY->get_industry_select());
					$smarty->assign('zipREX', 	$regex_zip);
					$smarty->assign('phoneREX', $regex_phone);
					$smarty->assign('emailREX', $regex_email);		
					$smarty->assign('title', 	'Admin: Update Selected Company');
					$smarty->assign('path', 	$site);
					$smarty->assign('body', 	'main/admin/updateCompany.tpl');
					$smarty->assign('companyID', $_POST['companyName']);
					$smarty->assign('name',		$name);
					$smarty->assign('industry',	$industry);
					$smarty->assign('address',	$address);
					$smarty->assign('city',		$city);
					$smarty->assign('state',	$state);
					$smarty->assign('zip',		$zip);
					$smarty->assign('address2',	$address2);
					$smarty->assign('city2',	$city2);
					$smarty->assign('state2',	$state2);
					$smarty->assign('zip2',		$zip2);
					$smarty->assign('phone',	$phone);
					$smarty->assign('fax',		$fax);
					$smarty->assign('website',	$website);
					$smarty->assign('email',	$email);
		
					$smarty->display('admin.tpl');
				}
			}
			else
			{
				$_SESSION['modID'] = $_POST['companyName'];
				$companyInfo = $EMPLOYER->get_company_info($_POST['companyName']);
					
				$name 		= $companyInfo['name'];
				$industry	= $companyInfo['industry'];
				$address 	= $companyInfo['address'];
				$city		= $companyInfo['city'];
				$state		= $companyInfo['state'];
				$zip		= $companyInfo['zip'];
				$address2	= $companyInfo['address2'];
				$city2		= $companyInfo['city2'];
				$state2		= $companyInfo['state2'];
				$zip2		= $companyInfo['zip2'];
				$phone		= $companyInfo['phone'];
				$fax		= $companyInfo['fax'];
				$website	= $companyInfo['website'];
				$email		= $companyInfo['email'];
			
				$smarty->assign('message', 	'');
			
				SmartyValidate::connect($smarty, true);
		
				SmartyValidate::register_validator('rv_companyName',	'companyName',			'notEmpty',	false, 	false, 'trim');
				SmartyValidate::register_validator('rv_address',		'address',				'notEmpty',	false, 	false, 'trim');
				SmartyValidate::register_validator('rv_city',		 	'city',					'notEmpty',	false, 	false, 'trim');
				SmartyValidate::register_validator('rv_zip',		 	'zip',					'isNumber',	false, 	false, 'trim');
				SmartyValidate::register_validator('rv_address2',	 	'address2',				'notEmpty',	true, 	false, 'trim');
				SmartyValidate::register_validator('rv_city2',		 	'city2',				'notEmpty',	true, 	false, 'trim');
				SmartyValidate::register_validator('rv_zip2',		 	'zip2',					'isNumber',	true, 	false, 'trim');
				SmartyValidate::register_validator('rv_email', 			'email', 				'isEmail', 	false, 	false, 'trim');
		
				$smarty->assign('states', 	get_state_select());
				$smarty->assign('industries', $INDUSTRY->get_industry_select());
				$smarty->assign('zipREX', 	$regex_zip);
				$smarty->assign('phoneREX', $regex_phone);
				$smarty->assign('emailREX', $regex_email);		
				$smarty->assign('title', 	'Admin: Update Selected Company');
				$smarty->assign('path', 	$site);
				$smarty->assign('body', 	'main/admin/updateCompany.tpl');
				$smarty->assign('companyID', $_POST['companyName']);
				$smarty->assign('name',		$name);
				$smarty->assign('industry',	$industry);
				$smarty->assign('address',	$address);
				$smarty->assign('city',		$city);
				$smarty->assign('state',	$state);
				$smarty->assign('zip',		$zip);
				$smarty->assign('address2',	$address2);
				$smarty->assign('city2',	$city2);
				$smarty->assign('state2',	$state2);
				$smarty->assign('zip2',		$zip2);
				$smarty->assign('phone',	$phone);
				$smarty->assign('fax',		$fax);
				$smarty->assign('website',	$website);
				$smarty->assign('email',	$email);
		
				$smarty->display('admin.tpl');
			}
		}
	}
}
?>